package com.alibaba.citrus.ots.price.ability.functions.factory.price.process;

import com.alibaba.acm.shaded.com.alibaba.metrics.StringUtils;
import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.api.customeraddressquery.service.CustomerAddressQueryService;
import com.epoch.app.otsplatformenhance.dto.request.CustomerAddressBatchRequest;
import com.epoch.app.otsplatformenhance.model.dto.BatchQueryAddress2Request;
import com.epoch.app.otsplatformenhance.model.dto.BatchQueryAddress2Response;
import com.epoch.app.otsplatformenhance.sdo.CustomerAddressSDO;
import com.epoch.app.otsplatformenhance.service.OtsPlatformEnhanceService;
import com.epoch.app.otspricecenter.enums.DimensionPropertyEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author hanguo
 * @date 2021/11/19
 * @Description 送达方
 */
@Component
public class SendAddressProcess extends PriceProcess {
    /**
     * 送达方地址查询服务
     */
    @Resource
    private CustomerAddressQueryService customerAddressQueryService;

    @Resource
    private OtsPlatformEnhanceService otsPlatformEnhanceService;

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainCodeAndDescMap(List<String> codeList, boolean innerCode) {
        List<CustomerAddressSDO> customerAddressSDOS = obtainCustomerAddressSDOList(codeList, innerCode);
        if (CollectionUtils.isEmpty(customerAddressSDOS)) {
            return new HashMap<>();
        }

        if (innerCode) {
            return customerAddressSDOS.stream().filter(item ->
                    StringUtils.isNotBlank(item.getAddressCode()) && StringUtils.
                            isNotBlank(item.getAddressName())).collect(Collectors.
                    toMap(CustomerAddressSDO::getAddressCode, CustomerAddressSDO::getAddressName, (x, y) -> x));
        }

        return customerAddressSDOS.stream().filter(item ->
                StringUtils.isNotBlank(item.getAddressCode()) && StringUtils.
                        isNotBlank(item.getAddressName())).collect(Collectors.
                toMap(CustomerAddressSDO::getAddressCode, CustomerAddressSDO::getAddressName, (x, y) -> x));
    }

    @Override
    public Map<String, String> obtainInnerCodeAndOuterCodeMap(List<String> codeList, boolean innerCode) {
        return new HashMap<>();
    }

    @RepositoryInvoker
    public List<CustomerAddressSDO> obtainCustomerAddressSDOList(List<String> codeList, boolean innerCode) {
        List<CustomerAddressSDO> customerAddressSDOS = Lists.newArrayList();

        codeList = dealCodeList(codeList);
        if (Objects.isNull(codeList)) {
            return customerAddressSDOS;
        }
//        CustomerAddressBatchRequest customerAddressBatchRequest = new CustomerAddressBatchRequest();
        BatchQueryAddress2Request batchQueryAddress2Request = new BatchQueryAddress2Request();
        ListUtils.partition(codeList, 15).forEach(e -> {
//            customerAddressBatchRequest.setAddressCodes(e);
//            customerAddressBatchRequest.setStatus(1);
//            Result<List<CustomerAddressSDO>> listResult = customerAddressQueryService.batchQueryCustomerAddress(customerAddressBatchRequest);

            batchQueryAddress2Request.setAddressCodes(e);
            BatchQueryAddress2Response listResult = otsPlatformEnhanceService.batchQueryAddress2(batchQueryAddress2Request);
            if (Objects.nonNull(listResult) && CollectionUtils.isNotEmpty(listResult.getResult())) {
                customerAddressSDOS.addAll(listResult.getResult());
            }
        });

        return customerAddressSDOS;
    }

    @Override
    public String getPropertyCode() {
        return DimensionPropertyEnum.CUSTOMER_SEND_ADDRESS_CODE.getKey();
    }
}
